package de.negativrekord.blockworld;

import cc.creativecomputing.control.CCControl;
import cc.creativecomputing.graphics.CCColor;
import cc.creativecomputing.graphics.CCGraphics;
import cc.creativecomputing.graphics.shader.CCGLSLShader;
import cc.creativecomputing.graphics.texture.CCTexture.CCTextureTarget;
import cc.creativecomputing.graphics.texture.CCTexture2D;
import cc.creativecomputing.graphics.texture.CCTextureData;
import cc.creativecomputing.io.CCIOUtil;
import cc.creativecomputing.math.CCMath;

import com.riekoff.particlecubeworld.player.AColorMaps;
import com.riekoff.particlecubeworld.player.APatterns;
import com.riekoff.particlecubeworld.player.AZones;

public class TileworldRenderer {

	@CCControl(name = "tile raster size", min = 0.001f, max = 1f)
	private float _cTileRasterWidth = 0.1f;
	
	@CCControl(name = "motar thickness", min = 0.0001f, max = 1f)
	private float _cMotarThickness = 0.01f;
	
	@CCControl(name = "motar smooth", min = 0.0001f, max = 1f)
	private float _cMotarSmooth = 0.01f;
	
	@CCControl(name = "motar edge smooth", min = 0.0001f, max = 0.01f)
	private float _cMotarEdgeSmooth = 0.01f;
	
	@CCControl(name = "motar hue", min = 0, max = 1)
	private float motarHue = 0;
	
	@CCControl(name = "motar saturation", min = 0, max = 1)
	private float motarSaturation = 0;
	
	@CCControl(name = "motar brightness", min = 0, max = 1)
	private float motarBrightness = 0;
	
	@CCControl(name = "pattern index", min = 0, max = 25)
	private int _cPatternIndex = 0;
	
	@CCControl(name = "color index", min = 0, max = 100)
	private int _cColorIndex = 0;
	
	@CCControl(name = "zone index", min = 0, max = 100)
	private int _cZoneIndex = 0;
	
	@CCControl(name = "zone scale", min = 0, max = 1)
	private float _cZoneScale = 0;
	
	@CCControl(name = "debug")
	public static boolean _cDebug = false;
	
	

	private CCTexture2D _myRandomTexture;
	private APatterns _myPatterns;
	private AColorMaps _myColorMaps;
	private AZones _myZones;
	private CCGLSLShader _myShader;
	
	float _myTime = 0f;
	
	public TileworldRenderer(CCGraphics g) {
//		_myShader = new CCGLSLShader(
//				CCIOUtil.classPath (ABrickShaderDemo.class, "tex_brick_vertex.glsl"),
//				CCIOUtil.classPath (ABrickShaderDemo.class, "tex_brick_fragment.glsl")
//		);
//		_myShader.load();
		
		CCTextureData myRandom = new CCTextureData(200,200);
		for(int x = 0; x < myRandom.width(); x++ ){
			for(int y = 0; y < myRandom.height(); y++ ){
				myRandom.setPixel(x, y, new CCColor(CCMath.random(),CCMath.random(),CCMath.random()));
			}
		}
		_myRandomTexture = new CCTexture2D(myRandom, CCTextureTarget.TEXTURE_RECT);
		
//		_myPatterns = new APatterns(g);
//		_myColorMaps = new AColorMaps();
//		_myZones = new AZones();
	}
	
	public void beginDraw (CCGraphics g) {g.clear();
	
		g.color(255);
		_myShader.start();
		g.texture(0, _myPatterns.texture());
		g.texture(1, _myZones.texture());
		g.texture(2, _myColorMaps.texture());
		
		g.texture(3,_myRandomTexture);
		
		float tileMotarWidth = 1;
		float motarStep = _cMotarThickness;
		
		_myShader.uniform1f("tileMotarWidth", tileMotarWidth);
		_myShader.uniform1f("motarStep", motarStep);
		_myShader.uniform1f("motarSmooth", _cMotarSmooth);
		_myShader.uniform1f("motarEdgeSmooth", _cMotarEdgeSmooth);
		
		_myShader.uniform4f("motarColor", CCColor.createFromHSB(motarHue, motarSaturation, motarBrightness));
		_myShader.uniform1f("patternScale", _cTileRasterWidth);
		_myShader.uniform1i("debug", _cDebug ? 1 : 0);
		
		_myShader.uniform1i("random", 3);
		_myShader.uniform1f("time", _myTime * 30);
		
		_myShader.uniform1i("patterns", 0);
		_myShader.uniform1i("patternIndex", _cPatternIndex);
		_myShader.uniform2fv("patternDimensions", _myPatterns.dimension());
		
		_myShader.uniform1i("zones", 1);
		_myShader.uniform1i("zoneIndex", _cZoneIndex);
		_myShader.uniform3fv("zoneDimensions", _myZones.zoneInfos());
		float[] zoneScales = new float[100];
		for(int i = 0; i < 100;i++){
			zoneScales[i] = _cZoneScale;
		}
		_myShader.uniform1fv("zoneScales",zoneScales);
		_myShader.uniform1i("colorMaps", 2);
		_myShader.uniform1i("colorIndex", _cColorIndex);
	
	}
	
	public void endDraw (CCGraphics g) {
		
		_myShader.end();
		g.noTexture();
		_myShader.end();
		
		g.color(255);}
}